/*
 * lookup.c
 *
 *  Created on: Oct 30, 2013
 *      Author: jiang
 */

#include <stdio.h>
#include "lookup.h"
#include "global.h"

/**
 * Kmer-Positions mapping table
 */

/////////////////////////////////////Below, using 1-distinct, array/////////////////////////////////////////////
/*IntiTable,Using array
 */

int initTableArray1(table_t **kmer_position_table,uint8_t **kmerStrandIndicator)
{
	int success = 1, fail = 0;
	*kmer_position_table = (position_t *) calloc(KMER_TABLE_SIZE,
				sizeof(position_t));
	*kmerStrandIndicator =(uint8_t *) calloc(KMER_TABLE_SIZE/8,
			sizeof(uint8_t));

	if ((*kmer_position_table == NULL) ||(*kmerStrandIndicator == NULL)) {
		fprintf(stderr, "[build table] insufficient memory!");
		exit(1);
	}
	//fprintf(stderr,"init success! \n");
	return success;

}

position_t getValueArray1(table_t *kmer_position_table,kmer_t kmer)
{
	return kmer_position_table[kmer];
}

void putValueArray1(table_t *kmer_position_table, kmer_t kmer, position_t pos)
{
	kmer_position_table[kmer] = pos;//handle with first kmer, POSITION_ZERO
}

int isEmptyArray1(table_t *kmer_position_table,kmer_t kmer) //
{

	if (kmer_position_table[kmer] == 0)
		return 1;
	else
		return 0;
}

int isNotuniqueArray1(table_t *kmer_position_table,kmer_t kmer) //
{

	if (kmer_position_table[kmer] == NOT_UNIQUE)
		return 1;
	else
		return 0;
}

int isUniqueArray1(table_t *kmer_position_table,kmer_t kmer) //
{

	if (kmer_position_table[kmer] != 0 && kmer_position_table[kmer] != NOT_UNIQUE)
		return 1;
	else
		return 0;
}

/////////////////////////////////////Below, using 2-distinct, array /////////////////////////////////////////////

/*
int initTableArray2(table_t **kmer_position_table)
{
		int success = 1, fail = 0;
		*kmer_position_table = (twoPositions_t *) calloc(KMER_TABLE_SIZE,
					sizeof(twoPositions_t));

		if (*kmer_position_table == NULL) {
			fprintf(stderr, "[build table] insufficient memory!");
			exit(1);
		}
		return success;
}

twoPositions_t getValueArray2(table_t *kmer_position_table,kmer_t kmer)
{
	return kmer_position_table[kmer];
}


int isEmptyArray2(table_t *kmer_position_table,kmer_t kmer)
{
	if (kmer_position_table[kmer].first == 0 && kmer_position_table[kmer].second ==0)
			return 1;
	else
		return 0;

}

int hasOneArray2(table_t *kmer_position_table, kmer_t kmer)
{

	if (kmer_position_table[kmer].first == 0 && kmer_position_table[kmer].second !=0)return 1;
	else if (kmer_position_table[kmer].first != 0 && kmer_position_table[kmer].second == 0 )return 2;
	else return 0;
}

int hasTwoArray2(table_t *kmer_position_table, kmer_t kmer)
{

	if (kmer_position_table[kmer].first != 0 && kmer_position_table[kmer].second != 0  && !(kmer_position_table[kmer].first == NOT_UNIQUE || kmer_position_table[kmer].second == NOT_UNIQUE)) return 1;
	else return 0;

}


static testLowHigh(table_t *kmer_position_table, kmer_t kmer)
{
		if (kmer_position_table[kmer].first == 0 && kmer_position_table[kmer].second ==0 )return 0;
		else if (kmer_position_table[kmer].first == 0 && kmer_position_table[kmer].second !=0)return 1;
		else if (kmer_position_table[kmer].first != 0 && kmer_position_table[kmer].second == 0 )return 2;
		else return 3;
}

void putValueArray2(table_t *kmer_position_table, kmer_t kmer, position_t  pos)
{
	int ret;
	ret = testLowHigh(kmer_position_table,kmer);
	if (ret == 0 || ret == 1) kmer_position_table[kmer].first = pos;
	else if (ret == 2) kmer_position_table[kmer].second = pos;
	else if (ret == 3)
		{ if (pos == NOT_UNIQUE)
			kmer_position_table[kmer].first = pos;
		 else fprintf(stderr,"[putValueArray2] Error! NOT_UNIQUE \n");
		}
	else fprintf(stderr,"[putValueArray2] Error! \n");
}


int isNotuniqueArray2(table_t *kmer_position_table,kmer_t kmer) //
{

	if (kmer_position_table[kmer].first == NOT_UNIQUE || kmer_position_table[kmer].second == NOT_UNIQUE)
		return 1;
	else
		return 0;
}

int isUniqueArray2(table_t *kmer_position_table,kmer_t kmer) //
{

	if (!(kmer_position_table[kmer].first == 0 && kmer_position_table[kmer].second == 0 )  && !(kmer_position_table[kmer].first == NOT_UNIQUE || kmer_position_table[kmer].second == NOT_UNIQUE))
		return 1;
	else
		return 0;
}*/

//////////////////////////////Below, using list////////////////////////////////////////////////

table_t* initTableList(char *name)
{

}

position_t getValueList(kmer_t kmer)
{

}

void putValueList(kmer_t kmer)
{

}

int testEmptyList(kmer_t kmer)
{


}






